package org.sky.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysUser;
import org.sky.sys.entity.SysUserRole;
import org.sky.sys.service.SysUserService;
import org.sky.sys.util.CriteriaUtils;
import org.sky.sys.util.RequestUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.util.JsonUtils;
import org.sky.util.LogUtils;
import org.sky.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author weifx
 * @Title: SysUserController
 * @ProjectName springboot-platform
 * @Description: TODO{用户管理}
 * @date 2019/3/8 13:38
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/sys/SysUser")
public class SysUserController {
    Logger log = LogUtils.getLogger(SysUserController.class);
    @Autowired
    private SysUserService sysUserService;

    /**
     * 打开菜单页面
     * @return
     */
    @GetMapping(value = "/initSysUserMngPage")
    public String initSysUserMngPage(){
        return "sys/user/sysusermng";
    }
    /**
     * 用户分页数据查询
     * @param request
     * @return
     */
    @PostMapping(value = "/selectSysUserByPage",produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String selectSysUserByPage(
            HttpServletRequest request){
        String filter = request.getParameter("filter");
        Map filterMap = JsonUtils.json2map(filter);
        String sortfield=request.getParameter("sortfield");
        int pageNum=Integer.parseInt(request.getParameter("page"));//当前第几页
        int pageSize=Integer.parseInt(request.getParameter("rows"));//每页显示条数
        //组织查询条件
        Example example= new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.initFilter(criteria, filterMap);
        if(!StringUtils.isBlank(sortfield)) {
            example.setOrderByClause(sortfield);
        }
        PageListData pageData = sysUserService.selectSysUserByPage(pageNum,pageSize,example);
        return JsonUtils.obj2json(pageData);
    }

    /**
     * 打开新增页面
     * @return
     */
    @GetMapping(value = "/initSysUserAddPage")
    public String initSysUserAddPage(){
        return "sys/user/sysuseredit";
    }

    /**
     * 打开编辑页面
     * @return
     */
    @GetMapping(value = "/initSysUserEditPage")
    public String initSysUserEditPage(){
        return "sys/user/sysuseredit";
    }

    /**
     * 打开明细页面
     * @return
     */
    @GetMapping(value = "/initSysUserDetailPage")
    public String initSysUserDetailPage(){
        return "sys/user/sysuserdetail";
    }
    /**
     *保存{新增/修改}用户
     **/
    @PostMapping(value = "/saveAddEditSysUser",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysUser(
            HttpServletRequest request){
        Result result = null;
        try {
            SysUser edit = RequestUtils.getEntityBean(request,SysUser.class);
            sysUserService.saveAddEditSysUser(edit);
            result = Result.success("保存成功");
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("保存失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }

    /**
     *根据主键查询组织机构表
     **/
    @GetMapping(value = "/getSysUserById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String getSysUserById(
            @PathVariable Long id,
            HttpServletRequest request){
        SysUser bean = sysUserService.getSysUserById(id);
        return JsonUtils.obj2json(bean);
    }
    @PostMapping(value = "/delSysUser",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysUser(
            HttpServletRequest request){
        Result result=null;
        try {
            String delRows=request.getParameter("delRows");
            List<SysUser> de=JsonUtils.json2list(delRows, SysUser.class);
            sysUserService.delSysUserById(de);
            result = Result.success("删除成功");
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("删除失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }

    /**
     * 密码重置
     * @param request
     * @return
     */
    @PostMapping(value = "/resetPwd",produces = "application/json;charset=UTF-8")
    public @ResponseBody String resetPwd(
            HttpServletRequest request){
        Result result = null;
        try {
            String resetRows=request.getParameter("resetRows");
            List reset=JsonUtils.json2list(resetRows, SysUser.class);
            sysUserService.resetPwd(reset);
            result = Result.success("重置成功");
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("重置失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }

    /**
     * 打开用户角色管理页面
     * @return
     */
    @GetMapping(value = "/initSysUserRoleMngPage")
    public String initSysUserRoleMngPage(){
        return "sys/user/sysuserrolemng";
    }

    /**
     * 分页查询用户角色
     * @param request
     * @return
     */
    @PostMapping(value = "/selectSysUserRoleByPage",produces = "application/json;charset=UTF-8")
    public @ResponseBody String selectSysUserRoleByPage(HttpServletRequest request){
        String filter = request.getParameter("filter");
        Map filterMap = JsonUtils.json2map(filter);
        String sortfield=request.getParameter("sortfield");
        int pageNum=Integer.parseInt(request.getParameter("page"));//当前第几页
        int pageSize=Integer.parseInt(request.getParameter("rows"));//每页显示条数
        //组织查询条件
        Example example= new Example(SysUserRole.class);
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.initFilter(criteria, filterMap);
        if(!StringUtils.isBlank(sortfield)) {
            example.setOrderByClause(sortfield);
        }
        PageListData pageListData = sysUserService.selectSysUserRoleByPage(pageNum,pageSize,example);
        return JsonUtils.obj2json(pageListData);
    }
    @RequestMapping(value = "/saveAddEditSysUserRoleList", method =RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysUserRoleList(
            HttpServletRequest request){
        Result result = null;
        try {
            String insertRows = request.getParameter("insertRows");
            String updateRows = request.getParameter("updateRows");
            List<SysUserRole> insert=null;
            List<SysUserRole> update=null;
            if(StringUtils.isNotBlank(insertRows)){
                insert = JsonUtils.json2list(insertRows, SysUserRole.class);
            }
            if(StringUtils.isNotBlank(updateRows)){
                update = JsonUtils.json2list(updateRows, SysUserRole.class);
            }
            sysUserService.saveAddEditSysUserRoleList(insert, update);
            result = Result.success("保存成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            result = Result.error("保存失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
    @PostMapping(value = "/delSysUserRole",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysUserRole(
            HttpServletRequest request){
        Result result = null;
        try {
            String delRows=request.getParameter("delRows");
            List de=JsonUtils.json2list(delRows, SysUserRole.class);
            sysUserService.delSysUserRole(de);
            result = Result.success("删除成功");
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("删除失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
    /**
     * 用户通用选择页面
     * @return
     */
    @GetMapping(value = "/initSysUserSelectPage")
    public String initSysUserSelectPage(){
        return "sys/user/sysuserselect";
    }
}
